package com.cyc.basic.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

/**
 * @description: 解析职位详细地址工具类
 * @version 1.0
 * @author cyc
 * @date 2021/12/2 10:31
 */
@Slf4j
public class JobAddressUtil {


    private static final Set<String> citySet = new HashSet<>();
    private static final Set<String> dqSet = new HashSet<>();

    static {
        try (InputStream inputStream = JobAddressUtil.class.getClassLoader().getResourceAsStream("data/city.txt")) {
            try (InputStreamReader reader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(reader)) {
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    citySet.add(line);
                }
            }
        } catch (IOException e) {
            log.error("解析城市失败");
        }
    }

    static {
        try (InputStream inputStream = JobAddressUtil.class.getClassLoader().getResourceAsStream("data/dq.txt")) {
            try (InputStreamReader reader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(reader);) {
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    dqSet.add(line);
                }
            }
        } catch (IOException e) {
            log.error("解析区失败");
        }
    }

    /**
     * 替换地址中的城市和区
     * @param address 原地址
     * @return String 新地址
     * @author cyc
     * @date 2021-12-02 11:24:36
     */
    public static String replacCityDq(String address) {
        if (StringUtils.isBlank(address)) {
            return address;
        }
        return checkDqAndReplace(checkCityAndReplace(address));
    }

    /**
     * 替换城市
     * @param address 原地址
     * @return String 新地址
     * @author cyc
     * @date 2021-12-02 11:28:04
     */
    public static String checkCityAndReplace(String address) {
        if (StringUtils.isBlank(address) || !address.contains("市") || address.length() < 4) {
            return address;
        }
        int cityIndex = address.indexOf("市") + 1;
        String city = address.substring(0, cityIndex);
        if (citySet.contains(city)) {
            address = address.substring(cityIndex);
        }
        return address;
    }

    /**
     * 替换区
     * @param address 原地址
     * @return String 新地址
     * @author cyc
     * @date 2021-12-02 11:28:13
     */
    public static String checkDqAndReplace(String address) {
        if (StringUtils.isBlank(address) || !address.contains("区") || address.length() < 4) {
            return address;
        }
        int dqIndex = address.indexOf("区") + 1;
        String dq = address.substring(0, dqIndex);
        if (dqSet.contains(dq)) {
            address = address.substring(dqIndex);
        }
        return address;
    }

    public static void main(String[] args) {
        String address = "北京市朝阳区来广营地铁站";
        address = checkCityAndReplace(address);
        System.out.println(address);
        address = checkDqAndReplace(address);
        System.out.println(address);
    }
}
